package com.atguigu.tingshu.album.api;


import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;


	@Autowired
	private VodService vodService;


	/**
	 * 将音频文件上传到腾讯云点播平台
	 *
	 * @param file 音频文件
	 * @return {mediaFileId:"文件唯一标识",mediaUrl:"播放地址"}
	 */
	@Operation(summary = "将音频文件上传到腾讯云点播平台")
	@PostMapping("/trackInfo/uploadTrack")
	public Result<Map<String,String>> uploadTrack(MultipartFile file){

		Map<String,String> mapResult = vodService.uploadTrack(file);

		return Result.ok(mapResult);
	}

	/**
	 * TODO:该接口需要登录才能访问
	 * 保存专辑下声音
	 *
	 * @param trackInfoVo 声音信息VO对象
	 * @return
	 */
	@TsLogin
	@Operation(summary = "保存专辑下声音")
	@PostMapping("/trackInfo/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo){

		Long userId = AuthContextHolder.getUserId();

		trackInfoService.saveTrackInfo(userId,trackInfoVo);

		return Result.ok();
	}

	/**
	 * TODO 必须登录才可以访问
	 * 获取当前登录声音分页列表
	 *
	 * @param page           页码
	 * @param limit          页大小
	 * @param trackInfoQuery 查询条件
	 * @return
	 */
	@TsLogin
	@Operation(summary = "获取当前登录声音分页列表")
	@GetMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
	public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable int page,
													   @PathVariable int limit,
													   @RequestBody TrackInfoQuery trackInfoQuery)
	{
		Long userId = AuthContextHolder.getUserId();

		// 获取封装分页查询对象
		trackInfoQuery.setUserId(userId);

		// 调用业务层进行分页
		Page<TrackListVo> pageInfo = new Page<>(page,limit);

		pageInfo = trackInfoService.getUserTrackPage(pageInfo,trackInfoQuery);

		return Result.ok(pageInfo);
	}


	/**
	 * 根据声音ID查询声音信息
	 *
	 * @param id
	 * @return
	 */
	@Operation(summary = "根据声音ID查询声音信息")
	@GetMapping("/trackInfo/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo (@PathVariable Long id){
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}


	/**
	 * TODO 该接口登录才可访问
	 * 修改声音信息
	 * @param id
	 * @param trackInfoVo
	 * @return
	 */
	@TsLogin
	@Operation(summary = "修改声音信息")
	@PutMapping("/trackInfo/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable Long id,@RequestBody @Validated TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(id,trackInfoVo);
		return Result.ok();
	}


	/**
	 * TODO 该接口登录才可访问
	 * 删除声音信息
	 * @param id
	 * @return
	 */
	@TsLogin
	@Operation(summary = "删除声音信息")
	@DeleteMapping("/trackInfo/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){

		trackInfoService.deleteTrackInfoById(id);

		return Result.ok();
	}


	/**
	 * 用于小程序端专辑页面展示分页声音列表，动态根据用户展示声音付费标识
	 *
	 * @param albumId
	 * @param page
	 * @param limit
	 * @return
	 */
	@TsLogin(required = false)
	@Operation(summary = "用于小程序端专辑分页展示分页声音列表，动态根据用户展示声音付费标识")
	@GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId,
															 @PathVariable Integer page,
															 @PathVariable Integer limit){
		Long userId = AuthContextHolder.getUserId();
		// 封装分页对象
		Page<AlbumTrackListVo> pageInfo = new Page<>(page,limit);
		// 调用业务层封装分页对象
		pageInfo = trackInfoService.getAlbumTrackPage(pageInfo,albumId,userId);

		return Result.ok(pageInfo);
	}

	/**
	 * 获取声音统计信息
	 * @param trackId
	 * @return
	 */
	@TsLogin(required = false)
	@Operation(summary = "获取声音统计信息")
	@GetMapping("/trackInfo/getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo (@PathVariable Long trackId){

		return Result.ok(trackInfoService.getTrackStatVo(trackId));
	}


}

